package com.atguigu.flink.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/7/12 11:12
 */
public class Flink06_SQL_Kafka {
    public static void main(String[] args) throws Exception {
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
       
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
       
        // 直接通过ddl方式建立动态表, 与文件关联
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts int, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092,hadoop163:9092', " +
                            "  'properties.group.id' = 'Flink06_SQL_Kafka', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv'" +
                            ")");
    
        Table table = tEnv.sqlQuery("select * from sensor where id='sensor_1'");
    
        tEnv.executeSql("create table sensor_out(" +
                            " id string, " +
                            " vc int, " +
                            " ts int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's2', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092,hadoop163:9092', " +
                            "  'format' = 'json' " +
                            ")");
        
        table.executeInsert("sensor_out");
        // sensor_1, 1000, 10
//        tEnv.executeSql("insert into sensor_out select * from " + table);
    
    }
}
